#ifndef _AREA_INTEGRATOR_HXX_
#define _AREA_INTEGRATOR_HXX_

#include <vector>
#include <map>

#include "GeoTiffImage.hxx"

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>
#include <string.h>

struct AILabel
{
    int Index;
    std::string Name;
    double Area;

    AILabel()
    {
        Index = 0;
        Name = "None";
        Area = 0.0;
    }

    AILabel(int idx, std::string name)
    {
        Index = idx;
        Name = name;
        Area = 0.0;
    }
};

class AreaIntegrator
{
public:
    AreaIntegrator();

    inline void setLabelFile(const char* labelFile) { strcpy(_labelFileName, labelFile); }
    inline void setDEMFile(const char* demFile) { strcpy(_demFileName, demFile); }
    inline void setOrthFile(const char* orthoFile) { strcpy(_orthoFileName, orthoFile); }

    inline void addLabel(AILabel label ) { _labels[label.Index] = label; }

    inline std::map<int, AILabel> getLabels() { return _labels; }

public: 
    bool computeAreas();

private:
    void computeLabelAreas();

    double computePixelArea(const int ix, const int iy, const double* upperRow, const double* centerRow, const double* lowerRow);

    double getDEMValue(float px, float py);

    void computeDEMRows(int rowIdx, double* upperRow, double* centerRow, double* lowerRow);
private:
    std::map<int, AILabel> _labels;

    char _labelFileName[256];
    char _demFileName[256];
    char _orthoFileName[256];

    GeoTiffImage* _orthoImage;
    GeoTiffImage* _demImage;

    cv::Mat _labelImage;
    cv::Mat _labelPalette;
};

#endif 
